package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyHistoryVideoList;
import com.yjt.fresh.api.ApiGetHistoryVideoList;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanHistory;
import com.yjt.fresh.bean.BeanHistoryHome;
import com.yjt.fresh.bean.BeanWalletDetail;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AtyHistoryVideoList extends BaseSimpleActivity {
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.tvTitle)
    TextView mTvTitle;
    @BindView(R.id.rvAppAtyHistoryVideos)
    RecyclerView mRv;
    @BindView(R.id.srlAppAtyHistoryVideos)
    SwipeRefreshLayout mSrl;
    private AdpRvAtyHistoryVideoList mAdpVideoList;
    private int mNextRequestPage = 1;
    private boolean isLoadMore;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_history_video_list;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        mTvTitle.setText(getIntent().getStringExtra(MyConstants.TITLE));
    }

    @Override
    protected void initEvent() {
        initRv();
        initClick();
        getData();
    }

    private void getData() {
        ApiGetHistoryVideoList apiGetHistoryVideoList = new ApiGetHistoryVideoList(new HttpOnNextListener<List<BeanHistory>>() {

            @Override
            public void onNext(List<BeanHistory> beanHistories) {
                if (isLoadMore) {
                    setData(false, beanHistories);
                } else {
                    setData(true, beanHistories);
                    mSrl.setRefreshing(false);
                }
            }

            @Override
            public void onReLogin() {

            }
        }, mContext, true, false, false);

        Map<String, Object> map = new HashMap<>();
        map.put("currentPage", mNextRequestPage);
        map.put("VIDEOTYPE_ID", getIntent().getStringExtra(MyConstants.VIDEO_TYPE_ID));
        apiGetHistoryVideoList.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetHistoryVideoList);

    }

    private void setData(boolean isRefresh, List<BeanHistory> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpVideoList.setNewData(data);
            mAdpVideoList.setEnableLoadMore(true);
        } else {
            if (size > 0) {
                mAdpVideoList.addData(data);
                mAdpVideoList.loadMoreComplete();
            } else {
                mAdpVideoList.loadMoreEnd(true);
                mAdpVideoList.setEnableLoadMore(false);
            }
        }

        if (size < 20) {
            mAdpVideoList.loadMoreEnd(true);
        }
    }

    private void initClick() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });

        mAdpVideoList.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        }, mRv);

        mAdpVideoList.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext,AtyHistoryVideos.class);
                BeanHistory beanHistory = mAdpVideoList.getItem(position);
                intent.putExtra(MyConstants.USER_ID, beanHistory.getSTORE_ID());
                intent.putExtra(MyConstants.CONTENT,beanHistory.getTITLE());
                intent.putExtra(MyConstants.TITLE,beanHistory.getTITLE());
                startActivity(intent);
            }
        });

    }

    private void initRv() {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        mRv.setLayoutManager(layoutManager);
        List<BeanHistory> list = new ArrayList<>();
        mAdpVideoList = new AdpRvAtyHistoryVideoList(R.layout.app_recycle_item_frg_history_home_content, list);
        mRv.setAdapter(mAdpVideoList);
    }
}
